<template>
  <div>
    <div class="app_card mb-2 pt-4 pb-4">
      <div class="d-flex justify-content-between search-form">
        <div class="flex-grow-1">
          <el-input clearable v-model="queryForm.name" class="w200px mr-3" placeholder="请输入姓名" />
          <el-input clearable v-model="queryForm.cardNo" class="w200px mr-3" placeholder="请输入卡ID" />
          <el-button type="primary" plain @click="onRefresh()">查询</el-button>
          <el-button type="info" @click="onReset()" link>重置</el-button>

        </div>
        <div class="d-flex">
          <!-- <el-button type="primary" @click="onAdd()">新增</el-button> -->
        </div>
      </div>
    </div>
    <div class="app_card pt-4  pb-5">
      <el-table v-loading="loading" element-loading-text="查询中..." stripe size="large" :data="dataOfTable"
        height="calc(100vh - 300px)" sortable="custom">

        <el-table-column width="300" label="姓名" prop="name" align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="学院" prop="college" align="center">
          <template #default="scope">{{ scope.row.college }}</template>
        </el-table-column>
        <el-table-column label="卡ID" prop="cardNo" align="center">
          <template #default="scope">{{ scope.row.cardNo }}</template>
        </el-table-column>
        <el-table-column label="卡状态" prop="status" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.status == 1 ? '启用' : '禁用' }}</template>
        </el-table-column>
        <!-- <el-table-column fixed="right" label="操作" width="120" align="left">
          <template #default="scope">
            <el-button link type="primary" @click="onEditRow(scope.row)">编辑
            </el-button>
            <el-button link type="primary" @click="onDeleteRow(scope.row)">删除
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <Pagination ref="paginationRef" @handleChange="handleChange" />
    </div>
    <EditDialog ref="editDialogRef" @success="loadData()"></EditDialog>
  </div>
</template>
  
<script setup>
/**
 * 一卡通管理
 */
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
//组件
import Pagination from '@/components/pagination/index.vue';
import EditDialog from './EditDialog.vue';
// ======================================================
//                      变量声明
// ======================================================
const loading = ref(false);

// 组件引用
const paginationRef = ref(null);
const editDialogRef = ref(null);
// 表格数据
const dataOfTable = ref([]);

// 表格查询参数
const queryForm = ref({
  name: "",
  cardNo: "",
})


// 表格查询参数
const queryParam = ref({
  query: "",
  sort: "Id",
  order: "asc",
  limit: 10,
  page: 1,
})


// ======================================================
//                      点击操作
// ======================================================
// 点击快速搜索按钮
const onReset = () => {
  queryForm.value.name = ""
  queryForm.value.cardNo = ""
  onRefresh()
}
// 点击刷新按钮
const onRefresh = () => {
  queryParam.value.page = 1;
  paginationRef.value?.changeTotal({ totalCount: 0 })
  loadData();
}


// 点击新增按钮
const onAdd = () => {
  editDialogRef.value.show(null, '添加');
}



// 点击行内编辑按钮
const onEditRow = (row) => {
  editDialogRef.value.show(row.id, '编辑 ', row);
}


// ======================================================
//                      处理事件
// ======================================================

// 点击行内删除按钮
const onDeleteRow = (row) => {
  ElMessageBox.confirm("确定要删除该记录吗？", "警告").then(() => {
    EsAjax.post(ApiUrls.iccardDelete, { ids: String(row.id) })
      .then(res => {
        if (res.data.code != 200) {
          ElMessage.warning(res.data.msg + res.data.data);
          return;
        }
        ElMessage.success('操作成功');
        loadData();
      })
      .catch(err => { ElMessage.error("网络错误：", err.message) });
  }).catch(() => {

  });
  console.log(row)
}
// 处理分页尺寸
const handleChange = (data = null) => {
  queryParam.value.page = data?.page;
  queryParam.value.limit = data?.limit;
  loadData();
}

// ======================================================
//                      处理事件
// ======================================================

const loadData = () => {
  let _queryParam = queryParam.value
  _queryParam.query = ''
  let queryList = []
  if (queryForm.value.name) {
    queryList.push(`name=${queryForm.value.name}`)
  }

  if (queryForm.value.cardNo) {
    queryList.push(`cardNo=${queryForm.value.cardNo}`)
  }
  if (queryList.length) {
    _queryParam.query = queryList.join(',')
  }

  _queryParam.offset = (_queryParam.page - 1) * _queryParam.limit || 0;
  loading.value = true
  EsAjax.post(ApiUrls.iccardDatagrid, { ...queryParam.value }).then(res => {
    if (res.data.code != 200) {
      ElMessage.warning(res.data.data + res.data.msg);
      return;
    }
    let { list, totalCount } = res.data.data
    dataOfTable.value = list;
    paginationRef.value?.changeTotal({ totalCount: totalCount })
  }).catch(err => {
    ElMessage.error(err.message);
  }).finally(() => {
    loading.value = false
  })
}

loadData();
</script>
  
